import group from '@/api/group.js'

const state = {
    group_list: undefined,
    apply_join_group_list: undefined,
    join_group_list: undefined,
    invite_join_group_list: undefined
}

const getters = {
    group_list: state => state.group_list,
    apply_join_group_list: state => state.apply_join_group_list,
    join_group_list: state => state.join_group_list,
    invite_join_group_list: state => state.invite_join_group_list
}

const actions = {
    getGroupList({
        commit
    }, params) {
        group.getGroupList(params).then(res => {
            commit('CHANGE_GROUP_LIST', res)
        })
    },
    async getApplyJoinGroupList({commit}){
        await group.getGroupApplyList().then(res => {
            commit('CHANGE_APPLY_JOIN_GROUP_LIST', res);
        });
    },
    getJoinGroupList({
        commit
    }, params) {
        group.getJoinGroupList(params).then(res => {
            commit('CHANGE_JOIN_GROUP_LIST', res);
        });
    },
    async getInviteJoinGroupList({commit}) {
        await group.getGroupInviteList().then(res => {
            commit('CHANGE_INVITE_JOIN_GROUP_LIST', res);
        });
    }
}

const mutations = {
    CHANGE_GROUP_LIST(state, data) {
        state.group_list = data;
    },
    CHANGE_APPLY_JOIN_GROUP_LIST(state, data){
        state.apply_join_group_list = data;
    },
    CHANGE_JOIN_GROUP_LIST(state, data) {
        state.join_group_list = data;
    },
    CHANGE_INVITE_JOIN_GROUP_LIST(state, data) {
        state.invite_join_group_list = data;
    }
}

export default {
    namespaced: true,
    state,
    getters,
    actions,
    mutations
}
